package com.ht.module.common.controller;

import com.ht.module.common.service.CommonService;
import com.ht.module.sys.service.ISysDictService;
import com.ht.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;


/**
 * @ProjectName: ht
 * @ClassName: commonController
 * @Author: hejialun
 * @Description: 公共类的控制器
 * @Date: 2021/9/14 15:50
 */
@RestController
@RequestMapping("/common")
@Api(tags = "公共类控制器 ")
public class CommonController {
    @Autowired
    private CommonService commonService;
    @Autowired
    private ISysDictService iSysDictService;

    @ApiOperation(value = "查询码表")
    @GetMapping("/getItem/{code}")
    public JsonResult getItem(@PathVariable String code) {
        return JsonResult.success(iSysDictService.getItem(code));
    }

    // 使用ConcurrentHashMap来存储每个id的锁对象
    private static final ConcurrentHashMap<String, ReentrantLock> lockMap = new ConcurrentHashMap<>();

    @ApiOperation(value = "测试接口")
    @GetMapping("/test/{id}")
    public JsonResult test(@PathVariable String id) {
        // 创建一个对象
        String data = new String("This is a soft reference example");

        // 创建一个软引用对象
        SoftReference<String> softReference = new SoftReference<>(data);

        // 获取软引用对象指向的实际对象
        System.out.println("Soft Reference Object: " + softReference.get());

        // 将原对象置为 null，方便进行 GC 测试
        data = null;

        // 手动触发垃圾回收
        System.gc();

        // 再次尝试从软引用中获取对象
        if (softReference.get() != null) {
            System.out.println("Object is still available");
        } else {
            System.out.println("Object has been collected by GC");
        }
        return JsonResult.success();
    }

}